package cn.lili.modules.grab.xdxt.service.impl;

import cn.lili.modules.grab.xdxt.config.XdxtConfig;
import cn.lili.modules.grab.xdxt.reps.order.CreateOrderReps;
import cn.lili.modules.grab.xdxt.reps.order.OrderFreightPreviewReps;
import cn.lili.modules.grab.xdxt.res.order.CreateOrderReq;
import cn.lili.modules.grab.xdxt.res.order.OrderFreightPreviewReq;
import cn.lili.modules.grab.xdxt.service.XdxtOrderServie;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.schl.api.utils.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.json.Json;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class XdxtOrderServieImpl implements XdxtOrderServie {

    @Resource
    private XdxtConfig xdxtConfig;

    /**
     * 运费计算
     *
     * @param orderFreightPreviewReq 请求内容
     */
    @Override
    public OrderFreightPreviewReps freightCalc(OrderFreightPreviewReq orderFreightPreviewReq) throws JsonProcessingException {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", xdxtConfig.getAppid());
        paramMap.put("secret", xdxtConfig.getSecret());
        paramMap.put("province", orderFreightPreviewReq.getProvince());
        paramMap.put("city", orderFreightPreviewReq.getCity());
        paramMap.put("district", orderFreightPreviewReq.getDistrict());
        paramMap.put("addressDetail", orderFreightPreviewReq.getAddressDetail());
        paramMap.put("orderDetailsList", orderFreightPreviewReq.getOrderDetailsList());
        log.info("freightCalc == {}", JSONObject.toJSONString(paramMap));

        JSONObject data = RequestUtil.orderFreightPreview(paramMap, xdxtConfig.getUrl());
        return OrderFreightPreviewReps.builderOrderFreightPreviewReps(data);
    }

    /**
     * 创建订单
     *
     * @param createOrderReq 请求内容
     */
    @Override
    public CreateOrderReps createOrder(CreateOrderReq createOrderReq) throws JsonProcessingException {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", xdxtConfig.getAppid());
        paramMap.put("secret", xdxtConfig.getSecret());
        paramMap.put("platformOrderNo", createOrderReq.getPlatformOrderNo());
        paramMap.put("custName", createOrderReq.getCustName());
        paramMap.put("custMobile", createOrderReq.getCustMobile());
        paramMap.put("province", createOrderReq.getProvince());
        paramMap.put("city", createOrderReq.getCity());
        paramMap.put("district", createOrderReq.getDistrict());
        paramMap.put("addressDetail", createOrderReq.getAddressDetail());
        paramMap.put("orderDetailsList", createOrderReq.getOrderDetailsList());
        log.info("推送订单 #createOrder#,请求参数paramMap:{}", JSONObject.toJSONString(paramMap));
        JSONObject data = RequestUtil.createOrder(paramMap, xdxtConfig.getUrl());
        log.info("推送订单 #createOrder#,响应结果,data:{}", JSONObject.toJSONString(data));
        return CreateOrderReps.builderCreateOrderReps(data);
    }
}
